<?php
namespace app\mobile\controller;

use app\model\Util; 
use app\model\Theme; 
use app\model\Fans; 
use app\model\Relation; 
use app\model\Scan; 

class OldFans extends Base
{
    public function Old_fans()
    {
global $_G, $_P;
$fans = $this->checkuser();
$system=$_G['system'];
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$themeid = intval($_P['themeid']);
$workerid = intval($_P['workerid']);
$p = array(":themeid"=>$themeid);
$pw = array(":workerid"=>$workerid);
$theme = Theme::getThemeById($themeid);
if(empty($theme['id'])){
    die(Util::showError("请确认活动地址是否正确"));
    return;
}
$config = 	Theme::getConfig($themeid);
// $cachekey = 'theme'.$_G['mpid'].MODULE_NAME.'-'.$themeid;
db_query("UPDATE ".tablename(t_theme)." SET scansum = scansum +1 WHERE id =:themeid",$p);
db_query("UPDATE ".tablename(t_worker)." SET scansum = scansum +1 WHERE uid =:workerid",$pw);
$theme['scansum'] = $theme['scansum']+1;
//cache_write($cachekey, $theme);
if(TIMESTAMP > $theme['endtime']){
    db_update(t_theme, array("status"=>2), array('id' => $themeid));
    $theme['status'] = 2;
    // cache_delete($cachekey);
}
if($op == 'check'){
	$params = array();
	
	$params[':openid']  = $_G['openid'];
	$params[':themeid'] = $themeid;
	$result['reload'] = 0;
	if(empty($_G['openid'])){
		$result['errcode'] = 1;
		$result['msg'] = 'openid is null';
		return_msg($result, '', 'ajax');
	}
	$member = db_find("SELECT * FROM ".tablename(t_fans)."
			WHERE themeid = :themeid and openid = :openid",$params);
	if(empty($member) && !empty($_G['member']['uid'])){
        $data = array(
            'themeid'     => $themeid,
            'uniacid'     => $_G['mpid'],
            'uid'         => $_G['member']['uid'],
            'openid'      => $_G['openid'],
            'nickname'    => $fans['nickname'],
            'avatar'      => empty($fans['avatar']) ? $fans['headimgurl']:$fans['avatar'],
            'status'      => 1,
            'ip'          => $_G['clientip'],
            'workerid'     => $workerid,
            'addtime'     => TIMESTAMP,

        );
        $insertid = db_insert(t_fans, $data);
	}else{
		if($member['status'] !=1){
			$result['errcode'] = 110;
			$result['msg'] = '你已被录入观察名单，你的IP、坐标、地址、微信信息已被记录在案';
			return_msg($result, '', 'ajax');
		}
	}
    $fromuid = intval($_P['fromuid']);
    $fopenid = $_P['fopenid'];
    if($fromuid == $_G['member']['uid'] || $fopenid == $_G['openid']){
        $fromuid = 0;
        $fopenid = "";
    }
    if($fromuid >0 && !empty($fopenid) || $workerid >0){
        $relation = Relation::getByThemeId($themeid,$_G['openid']);
        $data = array(
            'themeid'   => $themeid,
            'uniacid'   => $_G['mpid'],
            'uid'       => $_G['member']['uid'],
            'openid'    => $_G['openid'],
            'status'    => 1,
            'addtime'   => TIMESTAMP,
            'parentid'  => $fromuid,
            'popenid'   => $fopenid,
        );
        if ($system['hdget']==1 && Fans::getuid_old($fromuid,$themeid)) {
            $data['parentid_old']  = $fromuid;
            $data['popenid_old']  = $fopenid;
        }
        if(empty($relation)){
            if ($system['hdget']==1 &&Fans::getuid_old($workerid,$themeid)) {
                $data['workerid_old']  = $workerid;
            }
            $data['workerid'] = $workerid;
            $insertid = db_insert(t_relation, $data);
        }else{
            if($relation['popenid'] != $fopenid && $relation['isfrozen']!=1){
                Relation::update($relation['id'],$data);
            }
            if($relation['workerid'] != $workerid && $relation['isorder']!=1 && $workerid > 0){
                if ($system['hdget']==1 &&Fans::getuid_old($workerid,$themeid)) {
                    Relation::update($relation['id'],array('workerid' => $workerid,'workerid_old' => $workerid));
                }else{
                    Relation::update($relation['id'],array('workerid' => $workerid));
                }
            }
        }
    }
    $sacn = Scan::getByThemeId($themeid,$_G['openid']);
	if(empty($sacn)){
        $data = array(
            'themeid'   => $themeid,
            'uniacid'   => $_G['mpid'],
            'uid'       => $_G['member']['uid'],
            'openid'    => $_G['openid'],
            'nickname'  => $fans['nickname'],
            'avatar'    => empty($fans['avatar']) ? $fans['headimgurl']:$fans['avatar'],
            'status'    => 0,
            'addtime'   => TIMESTAMP,
            'fromuid'  => $fromuid,
            'fopenid'  => $fopenid,
            'workerid' => $workerid,
        );

        if ($system['hdget']==1) {
            if (Fans::getuid_old($fromuid,$themeid)) {
                $data['fromuid_old']  = $fromuid;
                $data['fopenid_old']  = $fopenid;
            }
            if (Fans::getuid_old($workerid,$themeid)) {
                $data['workerid_old']  = $workerid;
            }
        }
        if($config['iconlimit'] > 0){
            $todaynum = Scan::getFpenidTodayNum($themeid,$fopenid);
            if($todaynum < $config['iconlimit']){
                $data['status'] = 1;
            }
        }else{
            $data['status'] = 1;
        }
        $insertid = db_insert(t_scan, $data);
    }
    if($config['isyzmember']==1){
        $url = $_G['siteroot'] . 'addons/yun_shop/api.php?i='.$_G['mpid'].'&type=1&route=member.member.memberFromHXQModule';
        if($fromuid != $_G['member']['uid'] && $fopenid != $_G['openid']){
            //loadload_func('communication');
            $res = yxcurl($url, array('uid' =>$_G['member']['uid'],'mid' => $fromuid));
            //$result['res'] = $res;
        }
    }

	$result['errcode'] = 0;
	$result['msg'] = 'ok';

	return_msg($result, '', 'ajax');
}

}
}